<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElDropdown, ElDropdownMenu, ElDropdownItem, ElBadge } from 'element-plus'
import { useChatStore } from '@/utils/chat'
const router = useRouter()
const userStore = useUserStore()
const chatStore = useChatStore()
const isMenuOpen = ref(false)

const navigateTo = (path: string) => {
  router.push(path)
  isMenuOpen.value = false
}

// 登出功能
const handleLogout = () => {
  // 清除token
  localStorage.removeItem('token')
  userStore.logout()
  router.push('/login')
}

// 切换菜单状态
const toggleMenu = () => {
  isMenuOpen.value = !isMenuOpen.value
}
</script>

<template>
  <nav class="navbar">
    <div class="nav-container">
      <div class="nav-logo" @click="navigateTo('/')">
        <h1>朝物夕拾</h1>
      </div>
      
      <div class="nav-menu" :class="{ 'active': isMenuOpen }">
        <a href="#/" @click.prevent="navigateTo('/')">首页</a>
        <a href="#/create" @click.prevent="navigateTo('/create')">发布商品</a>
        <a href="#/profile" @click.prevent="navigateTo('/profile')">个人中心</a>
        
        <!-- 未登录状态 -->
        <template v-if="!userStore.isLogin">
          <a href="#/login" @click.prevent="navigateTo('/login')">登录</a>
          <a href="#/register" @click.prevent="navigateTo('/register')">注册</a>
        </template>
        
        <!-- 已登录状态 -->
        <template v-else>
          <el-dropdown>
            <span class="user-info">
              欢迎，{{ userStore.userName }}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="navigateTo('/profile')">个人中心</el-dropdown-item>
                <el-dropdown-item @click="navigateTo('/create')">发布商品</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">登出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          
          <a href="#/chat" @click.prevent="navigateTo('/chat')">
            <el-badge :value="chatStore.totalUnreadCount" :max="99" v-if="chatStore.totalUnreadCount > 0">
              聊天
            </el-badge>
            <span v-else>聊天</span>
          </a>
        </template>
      </div>
      
      <div class="nav-toggle" :class="{ 'active': isMenuOpen }" @click="toggleMenu">
        <span class="bar"></span>
        <span class="bar"></span>
        <span class="bar"></span>
      </div>
    </div>
  </nav>
</template>

<style scoped>
.navbar {
  background: #007bff;
  padding: 0;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
  width: 100%;
  margin: 0;
}

.nav-container {
  max-width: 100%;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
}

.nav-logo {
  cursor: pointer;
  flex-shrink: 0;
}

.nav-logo h1 {
  margin: 0;
  color: white;
  font-size: 1.5em;
}

.nav-menu {
  display: flex;
  gap: 30px;
  align-items: center;
}

.user-info {
  color: white;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  white-space: nowrap;
}

.user-info:hover {
  opacity: 0.8;
}

.nav-menu a {
  color: white;
  text-decoration: none;
  font-weight: 500;
  transition: opacity 0.3s ease;
  position: relative;
  white-space: nowrap;
}

.nav-menu a:hover {
  opacity: 0.8;
}

.unread-badge {
  background: #dc3545;
  color: white;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7em;
  position: absolute;
  top: -5px;
  right: -10px;
}

.nav-toggle {
  display: none;
  flex-direction: column;
  cursor: pointer;
  padding: 5px;
}

.bar {
  width: 25px;
  height: 3px;
  background: white;
  margin: 3px 0;
  transition: 0.3s;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .nav-container {
    padding: 0 15px;
    height: 50px;
  }
  
  .nav-logo h1 {
    font-size: 1.3em;
  }
  
  .nav-menu { 
    position: fixed;
    left: -100%;
    top: 50px;
    flex-direction: column;
    background: #007bff;
    width: 100%;
    text-align: center;
    transition: 0.3s;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    padding: 20px 0;
    gap: 15px;
    margin: 0;
  }

  .nav-menu.active { 
    left: 0;
  }
  
  .nav-menu a {
    padding: 10px 0;
    font-size: 1.1em;
  }
  
  .user-info {
    font-size: 1.1em;
    padding: 10px 0;
  }

  .nav-toggle { 
    display: flex;
  }

  .nav-toggle .bar:nth-child(2) { 
    opacity: 1;
  }
  
  /* 汉堡菜单动画 */
  .nav-toggle.active .bar:nth-child(1) {
    transform: translateY(8px) rotate(45deg);
  }
  
  .nav-toggle.active .bar:nth-child(2) {
    opacity: 0;
  }
  
  .nav-toggle.active .bar:nth-child(3) {
    transform: translateY(-8px) rotate(-45deg);
  }
}

@media (max-width: 480px) {
  .nav-container {
    padding: 0 10px;
    height: 50px;
  }
  
  .nav-logo h1 {
    font-size: 1.2em;
  }
  
  .nav-menu {
    padding: 15px 0;
    gap: 12px;
  }
  
  .nav-menu a {
    font-size: 1em;
  }
  
  .user-info {
    font-size: 1em;
  }
}
</style>